package apportioner

import (
	sqlc "bp/db/sqlc"
	"bp/internal/util"
)


func (s *ApportionerComServiceImp) genUpsertOffApportionmentParamsPtrsOfMultiple(
	offTransactionPtr *sqlc.OffTransaction,
	theMapOfOffTransactionDetail OffTrialItemToOffTransactionDetailMap,
	offTrialPtrs []*sqlc.OffTrial,
	offTrialItemPtrs []*sqlc.OffTrialItem,
) (
	upsertOffApportionmentParamsPtrs []*sqlc.UpsertOffApportionmentParams) {

	apportionScene := string(sqlc.EmOffApportionSceneOrder)
	apportionTypeMultiple := string(sqlc.EmOffApportionTypeMultiple)

	// 目前多品和单品优惠是互斥,可以用原价占比来分摊
	for _, offTrialPtr := range offTrialPtrs {
		offTrialItemPtrsOfTheOffTrial := s.entityHub.OffTrialService.FilterOffTrialItemPtrs(offTrialItemPtrs, func(oti *sqlc.OffTrialItem) bool {
			return oti.OffTrialID == offTrialPtr.ID
		})

		// 生成原始金额占比
		offTrialItemMap := s.ConvOffTrialItemPtrsToMap(offTransactionPtr.OwnerID, offTrialItemPtrsOfTheOffTrial)
		itemPriceAmountMap := s.genItemAmountDistribution(offTrialItemMap)

		// // 找出原始金额最高的试算项
		offTrialItemMaxItemAmount := s.findOffTrialItemByMaxItemAmount(offTrialItemPtrsOfTheOffTrial)
		offCompareKeyOfMaxItemAmount := s.ExtractOwnerOffTrialItemKey(offTransactionPtr.OwnerID)(offTrialItemMaxItemAmount)

		// 先计算排除最大占比的那些试算项目
		var offExcludeMaxItemAmount int32
		for _, offTrialItemPtr := range offTrialItemPtrsOfTheOffTrial {
			k := s.ExtractOwnerOffTrialItemKey(offTransactionPtr.OwnerID)(offTrialItemPtr)
			if k != offCompareKeyOfMaxItemAmount {
				offTransactionDetailPtrsOfMap := theMapOfOffTransactionDetail[k]
				off := int32(util.Fix0(float64(offTrialPtr.Off) * float64(itemPriceAmountMap[k]) / 1000))
				offExcludeMaxItemAmount += off
				for _, ptr := range offTransactionDetailPtrsOfMap {
					upsertOffApportionmentParamsPtrs = append(upsertOffApportionmentParamsPtrs, &sqlc.UpsertOffApportionmentParams{
						BrandID:                offTransactionPtr.BrandID,
						OwnerType:              offTransactionPtr.OwnerType,
						OwnerID:                offTransactionPtr.OwnerID,
						MemberID:               offTransactionPtr.MemberID,
						TransactionID:          offTransactionPtr.ID,
						OutTransNo:             offTransactionPtr.OutTransNo,
						OutOrderNo:             offTransactionPtr.OutOrderNo,
						OutAfterSaleNo:         offTransactionPtr.OutAfterSaleNo,
						OutOrderDetailID:       ptr.OutOrderDetailID,
						OutOrderDetailCnt:      ptr.OutOrderDetailCnt,
						OutAfterSaleDetailID:   ptr.OutAfterSaleDetailID,
						OutAfterSaleDetailCnt:  ptr.OutAfterSaleDetailCnt,
						ItemID:                 ptr.ItemID,
						ItemSpecID:             ptr.ItemSpecID,
						ItemPrice:              ptr.ItemPrice,
						ApportionScene:         apportionScene,
						ApportionType:          apportionTypeMultiple,
						OfferType:              offTrialPtr.OfferType,
						OfferItemType:          offTrialPtr.OfferItemType,
						OfferItemApportionInfo: offTrialPtr.OfferItemTrialInfo,
						Off:                    off,
						Cnt:                    offTrialItemPtr.Cnt,
					})
				}
			}
		}

		// 再计算最大占比的试算项目,用总优惠减去也有的优惠
		offOfMaxItemAmount := offTrialPtr.Off - offExcludeMaxItemAmount
		offTransactionDetailPtrsOfMapOfMaxItemAmount := theMapOfOffTransactionDetail[offCompareKeyOfMaxItemAmount]
		for _, ptr := range offTransactionDetailPtrsOfMapOfMaxItemAmount {
			upsertOffApportionmentParamsPtrs = append(upsertOffApportionmentParamsPtrs, &sqlc.UpsertOffApportionmentParams{
				BrandID:                offTransactionPtr.BrandID,
				OwnerType:              offTransactionPtr.OwnerType,
				OwnerID:                offTransactionPtr.OwnerID,
				MemberID:               offTransactionPtr.MemberID,
				TransactionID:          offTransactionPtr.ID,
				OutTransNo:             offTransactionPtr.OutTransNo,
				OutOrderNo:             offTransactionPtr.OutOrderNo,
				OutAfterSaleNo:         offTransactionPtr.OutAfterSaleNo,
				OutOrderDetailID:       ptr.OutOrderDetailID,
				OutOrderDetailCnt:      ptr.OutOrderDetailCnt,
				OutAfterSaleDetailID:   ptr.OutAfterSaleDetailID,
				OutAfterSaleDetailCnt:  ptr.OutAfterSaleDetailCnt,
				ItemID:                 ptr.ItemID,
				ItemSpecID:             ptr.ItemSpecID,
				ItemPrice:              ptr.ItemPrice,
				ApportionScene:         apportionScene,
				ApportionType:          apportionTypeMultiple,
				OfferType:              offTrialPtr.OfferType,
				OfferItemType:          offTrialPtr.OfferItemType,
				OfferItemApportionInfo: offTrialPtr.OfferItemTrialInfo,
				Off:                    offOfMaxItemAmount,
				Cnt:                    offTrialItemMaxItemAmount.Cnt,
			})
		}
	}

	return
}
